﻿using System;
using System.Web.Mvc;
using TimeTravel.Commons;
using TimeTravel.Components;

namespace TimeTravel.Controllers
{
    public class BaseController:Controller
    {
        protected string MainPagePath;
        protected string SubPagePath;
        public BaseController()
        {
            //获取当前配置的主页面路径
            if (String.IsNullOrEmpty(MainPagePath))
            {
                MainPagePath = GetValueFromCacheOrConfig(ConstantInfo.CacheConstant.MainPagePath, ConstantInfo.AppSettingConstant.MainPagePath);
            }
            //获取当前配置的子视图文件夹路径
            if (String.IsNullOrEmpty(SubPagePath))
            {
                SubPagePath = GetValueFromCacheOrConfig(ConstantInfo.CacheConstant.SubPagePath,ConstantInfo.AppSettingConstant.SubPagePath);
            }
        }

        /// <summary>
        /// 从缓存中获取数据
        /// </summary>
        /// <param name="cacheKey"></param>
        /// <param name="configKey"> </param>
        private string GetValueFromCacheOrConfig(string cacheKey,string configKey)
        {
            var cacheValue = CacheHelper.GetCache(cacheKey);
            if (cacheValue == null || String.IsNullOrEmpty(cacheValue.ToString()))
            {
                cacheValue = ConfigHelper.GetConfigString(configKey);
                CacheHelper.SetCache(cacheKey, cacheValue);
            }
            return cacheValue.ToString();
        }

        /// <summary>
        /// 处理未知请求
        /// </summary>
        /// <param name="actionName"></param>
        protected override void HandleUnknownAction(string actionName)
        {
            RedirectToAction("Page404", "Error");
        }
    }
}
